* CODE TO RUN ANALYSIS


* Create transfer rate results
clear all

* Need to merge results with industry
use "$result_simul/simulation_dataset.dta", clear

keep naics year shipments imports exports ei ei_tot te_2007 ei_cat te_cat ci_tvshare
tempfile temp
save `temp.dta', replace

* Preparing results data in wide format 
use  "$result_regs/regression_results.dta", clear

keep if yvar=="ln_val_prod" | yvar=="ln_val_imp" | yvar=="ln_val_exp"

drop obs

replace yvar="S" if yvar=="ln_val_prod"
replace yvar="X" if yvar=="ln_val_exp"
replace yvar="M" if yvar=="ln_val_imp"

* 1. rename models
replace bacon="none" if bacon==""
replace iv="ols" if iv==""
replace control="none" if control==""
replace othercontrol="none" if othercontrol==""
replace model="base" if model==""

* 2. keep relevant specifications
keep if iv=="iv"
keep if model=="lin_ei" | model=="lin_ei_lag"
keep if bacon=="bacon" | bacon=="none"
keep if naics_fe=="naics year"
drop if control=="ln_fep" | control=="L1.ln_fep"
drop if deflator=="qty"

reshape wide beta*, i(eivar bacon wt iv model control othercontrol deflator naics_fe ) j(yvar) string

gen spec = _n
gen year = 2007
order spec

save  "$result_regs/regression_results_wide.dta", replace

joinby year using `temp.dta'
isid spec naics
	
* generate transfer rates for each model and industry combination
gen transfer_rate = .

replace transfer_rate = -(betaM*imports - betaX*exports)/(betaS*shipments) if model == "base" | model == "lin_ei" | model == "lin_ei_lag"

replace transfer_rate = -((betaM + beta2M*ei)*imports - (betaX + beta2X*ei)*exports)/ ///
								((betaS + beta2S*ei)*shipments) if eivar=="ei" & model == "lin_ei2"
replace transfer_rate = -((betaM + beta2M*ei)*imports - (betaX + beta2X*ei)*exports)/ ///
								((betaS + beta2S*ei)*shipments) if eivar=="ei_tot"	 & model == "lin_ei2"								


* constrain transfer rates to be meaningful for policy
replace transfer_rate= 1 if transfer_rate>1
replace transfer_rate= 0 if transfer_rate<0

gen leakage_rate= ei * ci_tvshare * transfer_rate / 1000.0
replace leakage_rate= ei_tot * ci_tvshare * transfer_rate / 1000.0 if eivar=="ei_tot"

	
* consider smooth out transfer rate
gen tr_reg = .
gen tr_qreg = .
gen lr_reg = .
gen lr_qreg = .
qui foreach method in "reg" "qreg" {
	
	* smooth prediction across specifications
	foreach eivar in "ei" "ei_tot"  {
		`method' transfer_rate c.`eivar'##c.te_2007 [iw=shipments] if eivar=="`eivar'"
		replace tr_`method' = `eivar'*_b[`eivar'] + te_2007*_b[te_2007] ///
												+ te_2007*`eivar'*_b[c.`eivar'#c.te_2007] + _b[_cons] if eivar=="`eivar'"
	
		`method' leakage_rate c.`eivar'##c.te_2007 [iw=shipments] if eivar=="`eivar'"
		replace lr_`method' = `eivar'*_b[`eivar'] + te_2007*_b[te_2007] ///
												+ te_2007*`eivar'*_b[c.`eivar'#c.te_2007] + _b[_cons] if eivar=="`eivar'"									
	}
	
	* constrain predictions
	replace tr_`method'= 1 if tr_`method'>1
	replace tr_`method'= 0 if tr_`method'<0
	replace lr_`method'= 1 if tr_`method'>1
	replace lr_`method'= 0 if tr_`method'<0
		
}
	
* gen p50 transfer rate and average transfer rates within industry/eivar
bysort naics eivar: egen tr_p50 = pctile(transfer_rate), p(50)
bysort naics eivar: egen tr_avg = mean(transfer_rate)

* save industry level data
keep naics spec transfer_rate leakage_rate tr* lr*
compress
save "$result_regs/regression_results_tr_by_ind.dta", replace

// * some stats

// gen year = 2007 
// rename naics naics2012
// merge m:1 naics year using $EI/energy_intensity_industry_year.dta

// corr transfer_rate intensity
// corr tr_qreg intensity
// corr tr_p50 intensity
